<?php


require_once 'Objects/User.php';

/**
 * Tokens are handed out to authorized users. A token will be obtained when the user logs in, and each subsequent request from that user should contain the token.
 * Tokens are unique per IP address.
 */
class Token {
	private $tokenString;	// The actual token string.
	private $ipAddress;		// Integer representation of the token's IP address.
	private $firstLogin;	// UNIX timestamp of the first use for this token.
	private $lastLogin;		// UNIX timestamp for the last use of this token.
	private $user;			// The user object to which this token belongs.
	private $userID;		// User ID of the owner for this token.

	/**
	 * Gets the string representation of the token.
	 * 
	 * @return The token as a string.
	 */
	public function getTokenString() {
		return $this->tokenString;
	}
	/**
	 * Returns the IP address (as a string) associated with this token.
	 * 
	 * @return The IP address of the token.
	 */
	public function getIPAddress() {
		return $this->ipAddress;
	}
	/**
	 * Returns the UNIX timestamp for the first use of this token.
	 * 
	 * @return UNIX timestamp for the first use of this token.
	 */
	public function getFirstLogin() {
		return $this->firstLogin;
	}
	/**
	 * Gets the UNIX timestamp for when this token was last used.
	 * 
	 * @return UNIX timestamp for when this token was last used.
	 */
	public function getLastLogin() {
		return $this->lastLogin;
	}
	/**
	 * Gets the user object associated with this token.
	 * 
	 * @return The user attached to this token.
	 */
	public function getUser() {
		// Is the user object cached?
		if ($this->user instanceof User) {
			return $this->user;
			
		// It's not cached, but we can transform an ID into a User:
		} else if (is_numeric($this->userID)) {
			$user = UserTable::getByID($this->userID);
			if ($user instanceof User) {
				$this->user = $user;
			} else {
				$this->userID = null;
				$this->user = null;
			}
		}
		
		// Return result:
		return $this->user;
	}

	
	
	/**
	 * Sets a token string for this token.
	 * 
	 * @param tokenString the new tokenString.
	 */
	public function setTokenString($tokenString) {
		$this->tokenString = $tokenString;
	}
	/**
	 * Sets the IP address associated with this token. The given IP should be a string.
	 * 
	 * @param ipAddress The new IP address.
	 */
	public function setIPAddress($ipAddress) {
		$this->ipAddress = ip2long($ipAddress);
	}
	/**
	 * Sets the UNIX timestamp indicating the first use for this token.
	 * 
	 * @param firstLogin The new firstLogin UNIX timestamp.
	 */
	public function setFirstLogin($firstLogin) {
		$this->firstLogin = $firstLogin;
	}
	/**
	 * Sets the UNIX timestamp for when this token was last used.
	 * 
	 * @param lastLogin The new lastLogin UNIX timestamp.
	 */
	public function setLastLogin($lastLogin) {
		$this->lastLogin = $lastLogin;
	}
	/**
	 * Sets the user ID associated with this token.
	 * 
	 * @param userID The new userID.
	 */
	public function setUserID($userID) {
		// Check if we have to reset the user object:
		$this->userID = $userID;
		if (!($this->user instanceof User) or ($this->user->getID() != $userID)) {
			$this->user = null;
		}
	}
	/**
	 * Sets the user object associated with this token.
	 * 
	 * @param user The new User object.
	 */
	public function setUser(User $user) {
		$this->user = $user;
	}
}
?>
